﻿using System;
using System.Data;
using System.Data.SqlClient;
using System.Web;
using tmAsk.Common;

namespace tmAsk.Webs
{
    public partial class B_ask : System.Web.UI.Page
    {
        tmAsk.Common.function make_htm = new tmAsk.Common.function();
        tmAsk.Data.MakehtmlSQL SQL = new tmAsk.Data.MakehtmlSQL();        

        protected void Page_Load(object sender, EventArgs e)
        {
            tmAsk.Data.MakehtmlSQL SQL = new tmAsk.Data.MakehtmlSQL();
            string id = Request.Params["id"];
            string key = Request.Params["k"];
            string classid = "0";

            uphitNumber(id);

            DataTable rs = SQL.GetList("b_ask", "a_id = " + id, "a_classid,a_ischeck").Tables[0];
            if (Convert.ToInt32(rs.Rows[0]["a_ischeck"]) == 0)
            {
                if (rs.Rows.Count > 0)
                {
                    classid = rs.Rows[0][0].ToString();
                }
                Response.Write((new function()).Gettemplate(8, "[id]|[classid]", id + "|" + classid));

                if (function.CheckNull(key))
                {
                    (new alldata()).update_search(key, id);
                }
            }
            else
            {
                Response.Write("无此问题，或问题未通过审核。");
            }
        }

        private void uphitNumber(string askid)
        {
            if (HttpContext.Current.Request.Cookies["client"] == null)
            {
                uphitNumberSQL(askid);

                tmAsk.Common.function make_htm = new tmAsk.Common.function();
                HttpCookie clientcookie = new HttpCookie("client");
                clientcookie.Values.Add("ip", make_htm.Md5(tmAsk.Common.function.getClientIP()));
                clientcookie.Values.Add("avh", askid);
                clientcookie.Expires = DateTime.Now.AddDays(1);
                HttpContext.Current.Response.AppendCookie(clientcookie);
            }
            else
            {
                HttpCookie cookie = Request.Cookies["client"];

                if (cookie.Values["avh"].Contains(askid) == true)
                {
                    if (HttpContext.Current.Request.Cookies["client"]["ip"].ToString() == make_htm.Md5(function.getClientIP()))
                    {
                        return;
                    }
                    else
                    {
                        uphitNumberSQL(askid);
                    }
                }
                else
                {
                    uphitNumberSQL(askid);

                    tmAsk.Common.function make_htm = new tmAsk.Common.function();
                    HttpCookie clientcookie = new HttpCookie("client");
                    clientcookie.Values.Add("ip", make_htm.Md5(tmAsk.Common.function.getClientIP()));
                    clientcookie.Values.Add("avh", cookie.Values["avh"] + "," + askid);
                    clientcookie.Expires = DateTime.Now.AddDays(1);
                    HttpContext.Current.Response.AppendCookie(clientcookie);
                }
            }
        }


        private void uphitNumberSQL(string id)
        {
            string sql = "update b_ask set a_hitNumber=a_hitNumber+1 where a_id=@a_id";
            SqlParameter[] eparameters = new SqlParameter[]{
                new SqlParameter("@a_id", SqlDbType.Char),
            };
            eparameters[0].Value = id;
            SQL.ExecuteSql(sql, eparameters);
        }
    }
}